﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Utils.Wpf.Chart
{
	public class ChartAxisCollection : INotifyPropertyChanged
	{
		#region Attributes

		private readonly ObservableCollection<ChartAxis> _axis;
		private string _maxYCaption;
		private string _minYCaption;

		#endregion

		#region Properties

		public string MaxYCaption
		{
			get
			{
				return _maxYCaption;
			}
			set
			{
				if (_maxYCaption != value)
				{
					_maxYCaption = value;
					this.OnPropertyChanged("MaxYCaption");
				}
			}
		}

		public string MinYCaption
		{
			get
			{
				return _minYCaption;
			}
			set
			{
				if (_minYCaption != value)
				{
					_minYCaption = value;
					this.OnPropertyChanged("MinYCaption");
				}
			}
		}

		public IList<ChartAxis> Items
		{
			get { return _axis; }
		}

		#endregion

		#region Public Methods

		public ChartAxisCollection()
		{
			_axis = new ObservableCollection<ChartAxis>();
			_axis.CollectionChanged += Axis_CollectionChanged;
		}

		#endregion

		#region Event Handlers

		private void Axis_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
			{
				ChartAxis newAxis = (ChartAxis)e.NewItems[0];
				newAxis.PropertyChanged += NewChart_PropertyChanged;
			}
			else
			{
				Debug.WriteLine("Unexpected action in ChartAxisCollection. Action: " + e.Action.ToString());
			}
		}

		private void NewChart_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			string newCaption;
			// Rebuild the whole caption 
			if (e.PropertyName == "MaxY")
			{
				newCaption = null;
				for (int index = 0; index < _axis.Count; index++)
				{
					if (_axis[index].MaxY != null)
						newCaption += FormatNumberWithChartPrecission(_axis[index].MaxY.Value);
					if (index < _axis.Count - 1)
						newCaption += Environment.NewLine;
				}
				this.MaxYCaption = newCaption;
			}
			else if (e.PropertyName == "MinY")
			{
				newCaption = null;
				for (int index = 0; index < _axis.Count; index++)
				{
					if (_axis[index].MinY != null)
						newCaption += FormatNumberWithChartPrecission(_axis[index].MinY.Value);
					if (index < _axis.Count - 1)
						newCaption += Environment.NewLine;
				}
				this.MinYCaption = newCaption;
			}
		}

		#endregion

		#region Private Methods

		private static string FormatNumberWithChartPrecission(decimal number)
		{
			if (number > 100)
			{
				return number.ToString("#");
			}
			else if (number > 10)
			{
				return number.ToString("#.#");
			}
			else
			{
				return number.ToString("#.##");
			}
		}

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		#endregion

	}
}
